package com.example.match.service;

import com.example.match.model.domain.User;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.StopWatch;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author 雨后天晴
 * @version 1.0
 * @since 2024/9/22 18:56
 */
@SpringBootTest
public class InsertUserTest {

    @Resource
    private UserService userService;

    // CPU 密集型：分配的核心线程数 = CPU 核心数 - 1
    // IO 密集型：分配的核心线程数可以大于 CPU 核心数
    private ExecutorService executorService = new ThreadPoolExecutor(60, 1000, 10000, TimeUnit.MINUTES, new LinkedBlockingQueue<>(1000));

    /**
     * 批量插入用户
     */
    @Test
    public void doInsertUser() {
        StopWatch stopWatch = new StopWatch();
        System.out.println("good........................");
        stopWatch.start();
        final int INSERT_COUNT = 1000;
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < INSERT_COUNT; i++) {
            User user = getUser();
            userList.add(user);

        }
        userService.saveBatch(userList, 100);
        stopWatch.stop();
        System.out.println("耗时：" + stopWatch.getTotalTimeMillis());
    }

    /**
     * 批量插入用户
     */
    @Test
    public void doConcurrencyInsertUser() {
        StopWatch stopWatch = new StopWatch();
        System.out.println("good........................");
        stopWatch.start();
        // final int INSERT_COUNT = 100000;
        int batchSize = 1000;
        int j = 0;
        List<CompletableFuture<Void>> futureList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            List<User> userList = new ArrayList<>();
            User user = getUser();
            do {
                j++;
                userList.add(user);
            } while (j % batchSize != 0);
            CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                System.out.println("线程：" + Thread.currentThread().getName());
                userService.saveBatch(userList, 10000);
            }, executorService);
            futureList.add(future);
        }
        // 异步执行
        CompletableFuture.allOf(futureList.toArray(new CompletableFuture[]{})).join();
        stopWatch.stop();
        // 耗时：6357
        System.out.println("耗时：" + stopWatch.getTotalTimeMillis());
    }

    private static User getUser() {
        User user = new User();
        user.setUsername("琴");
        user.setUserAccount("qinTuanZhang");
        user.setAvatarUrl("https://tse3-mm.cn.bing.net/th/id/OIP-C.14dOBeIprD1fISRG4HSATAAAAA?rs=1&pid=ImgDetMain");
        user.setGender(1);
        user.setProfile("西风骑士团团长");
        user.setUserPassword("e6d1998fb96a1bb0e99538d4ac370f9b");
        user.setPhone("158388");
        user.setEmail("172688");
        user.setTags("[\"Java\",\"C++\",\"Python\"]");
        user.setUserStatus(0);
        user.setUserRole(0);
        user.setPlanetCode("8");
        return user;
    }
}
